package com.ndood.spring.boot.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

/**
 * 安全配置类
 * @author qilongfei
 *
 */
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter{

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http.authorizeRequests()
			.antMatchers("/css/**","/js/**","/fonts/**","/index/**").permitAll()// 都可以访问
			.antMatchers("/users/**").hasRole("ADMIN")// 需要相应的角色才可以访问
			.and()
			.formLogin() // 基于form表单登录认证
			.loginPage("/login").failureUrl("/login-error"); // 自定义登录界面
	}

	/**
	 * 认证信息管理
	 */
	@Autowired
	public void configureGrobal(AuthenticationManagerBuilder auth) throws Exception{
		auth.inMemoryAuthentication()
			.withUser("ndood").password("123456").roles("ADMIN");
	}
}
